<template>
  <page-header-wrapper>
    <global-search ref="globalSearch" :list="searchList" @search="handleSearch" @reset="handleReset"> </global-search>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        :rowKey="(record, index) => index"
        :columns="columns"
        :data="loadData"
        :ablePrint="false"
        :customExport="customExport"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <y-button
          slot="tabAction"
          type="warning"
          plain
          icon="pie-chart"
          theme="filled"
          @click="handleViewChart"
        >查看图表</y-button
        >
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
      </s-table>
    </a-card>
    <chart :visible.sync="visibleChart" :queryParam="queryParam"></chart>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getSubsidyStatement } from '@/api/statementAndStatistics'
import chart from './components/chart.vue'
import moment from 'moment'
import BigNumber from 'bignumber.js'
export default {
  components: {
    STable,
    Ellipsis,
    chart
  },
  provide () {
    return {
      localDataSource: () => {
        const obj = {}
        const keyArr = [
          'QuarterProposer',
          'QuarterDetail',
          'QuarterMoney',
          'QuarterNewProposer',
          'QuarterNewDetail',
          'QuarterNewMoney',
          'QuarterExitProposer',
          'QuarterExitDetail'
        ]
        keyArr.forEach((key) => {
          let num = new BigNumber(0)
          this.localDataSource.forEach((e) => {
            num = num.plus(e[key])
          })
          obj[key] = num.toNumber()
        })
        return obj
      },
      title: () => {
        const { year, quarter } = this.queryParam
        return `${year}年第${quarter}季度租赁补贴保障明细`
      }
    }
  },
  computed: {
    columns () {
      const { year, quarter } = this.queryParam
      return [
        {
          title: `县市区`,
          align: 'center',
          dataIndex: 'DeptName'
        },
        {
          title: `上年度`,
          align: 'center',
          children: [
            {
              title: `租赁补贴发放(户数)`,
              align: 'center',
              dataIndex: 'LastYearProposer'
            },
            {
              title: `累计发放租赁补贴金额(万元)`,
              align: 'center',
              dataIndex: 'LastYearMoney'
            }
          ]
        },
        {
          title: `${year}年第${quarter}季度低收入住房保障情况统计表`,
          align: 'center',
          children: [
            {
              title: `季度末正在实施保障情况`,
              align: 'center',
              children: [
                {
                  title: `户数`,
                  align: 'center',
                  dataIndex: 'QuarterProposer'
                },
                {
                  title: `人数`,
                  align: 'center',
                  dataIndex: 'QuarterDetail'
                },
                {
                  title: `发放金额(万元)`,
                  align: 'center',
                  dataIndex: 'QuarterMoney'
                }
              ]
            },
            {
              title: `本季度新增纳入保障情况`,
              align: 'center',
              children: [
                {
                  title: `户数`,
                  align: 'center',
                  dataIndex: 'QuarterNewProposer'
                },
                {
                  title: `人数`,
                  align: 'center',
                  dataIndex: 'QuarterNewDetail'
                },
                {
                  title: `发放金额(万元)`,
                  align: 'center',
                  dataIndex: 'QuarterNewMoney'
                }
              ]
            },
            {
              title: `本季度退出保障情况`,
              align: 'center',
              children: [
                {
                  title: `户数`,
                  align: 'center',
                  dataIndex: 'QuarterExitProposer'
                },
                {
                  title: `人数`,
                  align: 'center',
                  dataIndex: 'QuarterExitDetail'
                }
              ]
            }
          ]
        },
        {
          title: `本年度`,
          align: 'center',
          children: [
            {
              title: `累计发放租赁补贴金额(万元)`,
              align: 'center',
              dataIndex: 'YearMoney'
            },
            {
              title: `新增租赁补贴发放(户数)`,
              align: 'center',
              dataIndex: 'YearNewProposer'
            },
            {
              title: `退出租赁补贴发放(人数)`,
              align: 'center',
              dataIndex: 'YearExitProposer'
            }
          ]
        }
      ]
    }
  },
  data () {
    return {
      visibleChart: false,
      localDataSource: [],
      searchList: [
        {
          label: '年度',
          fields: 'year',
          type: 'year',
          initialValue: moment().year(),
          props: {
            placeholder: '请选择年度'
          }
        },
        {
          label: '季度',
          fields: 'quarter',
          type: 'select',
          initialValue: moment().quarter(),
          props: {
            placeholder: '请选择季度',
            options: [
              {
                label: '第一季度',
                value: 1
              },
              {
                label: '第二季度',
                value: 2
              },
              {
                label: '第三季度',
                value: 3
              },
              {
                label: '第四季度',
                value: 4
              }
            ]
          }
        }
      ],
      queryParam: {
        Mode: 'QuarterStatementCategory',
        year: moment().year(),
        quarter: moment().quarter(),
        UserID: this.$store.getters.userid
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getSubsidyStatement(requestParameters).then((res) => {
          this.localDataSource = res.data
          return res
        })
      }
    }
  },
  methods: {
    handleSearch (values) {
      Object.assign(this.queryParam, values)
      this.tableRefresh(true)
    },
    handleReset () {
      Object.assign(this.queryParam, {
        year: moment().year(),
        quarter: moment().quarter()
      })
      this.tableRefresh(true)
    },
    tableRefresh (refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    customExport (data, columns) {
      this.$message.warning('开发中...')
    },
    handleViewChart () {
      this.visibleChart = true
    }
  }
}
</script>
<style scoped>
</style>
